<?php

class ShelfController extends Zend_Controller_Action
{   
    public function init()
    {
        $this->stylesBase = $this->view->baseUrl() . '/styles/';
    }

    public function indexAction()
    {         
        $view = $this->_getView('ViewShelf');  
        $view->department = $this->_getAllDepartments();         
    }
    
    public function viewshelfAction()
    {         
        $view = $this->_getView('ViewShelf');  
        $id = (string)$this->getRequest()->getParam('id'); 
        $view->id = $id;
        $view->department = $this->_getAllDepartments();        
        $view->shelf = $this->_getAllShelfs();               
        //$view->shelfid = $this->_getViewShelf($id);       
    }
    
    public function editAction()
    {   
        $view = $this->_getView('EditDepartment');
        $form = $this->_getEditShelfForm();
       
        if ($this->getRequest()->isPost()) 
	{
            $formData  = $this->_request->getPost();
            if ($form->isValid($formData)) 
            {
                $view = $this->_getView('Fout - Toevoegen');                
                // Shelf toevoegen 
                $values = $form->getValues();
                $view->shelfDebug = $values;
                
                // Set only the interesting values
                $shelf_values['id']     = $values['shelf_id'];
                $shelf_values['label']  = $values['shelf_label'];
                $shelf_values['x']      = $values['shelf_x'];
                $shelf_values['y']      = $values['shelf_y'];
                $shelf_values['h']      = $values['shelf_h'];
                $shelf_values['w']      = $values['shelf_w'];
                $shelf_values['alpha']  = $values['shelf_alpha'];
                
                // Let's save *yay*
                $shelf = new Application_Model_Shelf($shelf_values);
                $shelfMapper = new Application_Model_ShelfMapper();
                $shelfMapper->save($shelf);

                return $this->_helper->redirector('');                
            }
            
        }else{
            $id = (string)$this->getRequest()->getParam('id');

            $shelf = $this->_getViewShelf($id);
            $view->shelf = $shelf;    
            $form->populate($shelf->toArray());     
            $view->form = $form; 
        } 
    }    
    
    /**
     * @param string $title
     * @return Zend_View
     */
    private function _getView($title)
    {
        $view = $this->view;
        $view->title = $title;
        $view->headTitle($view->title, 'prepend');                           // Zend_View_Helper_HeadTitle
        $view->headLink()->appendStylesheet($this->stylesBase . 'main.css'); // Zend_View_Helper_HeadLink

        return $view;
    }
    
      /**
     * @return Array
     */
    private function _getAllShelfs()
    {
        $shelfMapper = new Application_Model_ShelfMapper();
        $shelfString= $shelfMapper->fetchAll();

        return $shelfString;
    }
    
    private function _getEditShelfForm()
    {   
        return new Application_Form_EditShelf(array(
            'action' => $this->view->baseUrl() . '/shelf/edit/',
            'post'   => 'post',
        ));
    }
    
    private function _getViewShelf($id)
    {    
        $shelfMapper = new Application_Model_ShelfMapper();
        $shelfString= $shelfMapper->fetchById($id);

        return $shelfString;
    }
    
      /**
     * @return Array
     */
    private function _getAllDepartments()
    {
        $departmentMapper = new Application_Model_DepartmentMapper();
        $departmentString= $departmentMapper->fetchAll();

        return $departmentString;
    }
}

